// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'

/**
 * https://github.com/pagekit/vue-resource
 */
import VueRosuorce from 'vue-resource'

/**
 * https://monterail.github.io/vuelidate/
 */
import Vuelidate from 'vuelidate'

/**
 * https://github.com/shakee93/vue-toasted
 */
import VueToasted from "vue-toasted"

import Flexible from 'lib-flexible'

import 'normalize.css'

/**
 * 页面入口
 */
import App from './App'

/**
 * 路由统一配置
 */
import router from './router'

/**
 * vuex store
 */
import store from './store'

/**
 * vue 全局过滤器
 */
import Filter from '@/extends/vue/filter.js'

/**
 * 扩展原生方法
 */
import Prototype from '@/extends/module/prototype.js'

/**
 * 接口地址统一管理文件
 */
import Api from '@/extends/module/api.js'

/**
 * 全局公用方法
 */
import Utils from '@/extends/module/utils.js'

/**
 * 和飞信客户端API接口
 */
import FXAPI from "@/extends/module/fxAPI.js"


/**
 * 全局toast引入
 */
import Toast from "@/extends/module/toast.js"

// import "@/scss/index.scss"

/**
 * 插件全局注册
 */
Vue.use(VueRosuorce)
Vue.use(Vuelidate)
Vue.use(VueToasted, {
	fullWidth: false,
	fitToScreen: false,
	theme: "primary",
	position: "bottom-center",
	duration : 2000
})
Vue.use(FXAPI)


Vue.use(Toast);


// 挂载全局变量
Vue.prototype.bus = new Vue()
Vue.prototype.api = Api
Vue.prototype.utils = Utils

/**
 * 对路由进行全局登录判断
 */
router.beforeEach((to, from, next) => {

	// 访问登录页或已登录直接跳转
	if (to.name == 'login' || Vue.prototype.cache) {
		next();
		return;
	}

	// 获取用户信息
	Vue.http.post(Api.eabcommon, {
		action: 'getUserAndEnterpriseInfo'
	}, {
		emulateJSON: true
	}).then(
		res => {
			if (res.body.result === 1) {
				let enterprises = res.body.enterprises;
				let defaultEnterprise = null;

				for (let i=0; i<enterprises.length; i++) {
					let enterprise = enterprises[i];
					if (res.body.data.enterpriseId == enterprise.enterpriseId) {
						defaultEnterprise = enterprise;
					}
				}

				Vue.prototype.cache = {
					userId: res.body.data.qy_user_id,
					mobile: res.body.data.mobile,
					defaultEnterprise: defaultEnterprise,
					enterprises: enterprises,
					roleIds: res.body.roles ? res.body.roles.roleIdList : []
				}

				next();
			} else {
				// Vue.toasted.show(res.body.errorMsg);
                vm.$toast.show({
                    text:res.body.errorMsg,
                    translate:true
                })
			}
		}
	);
});

Vue.config.productionTip = false

/* eslint-disable no-new */
window.vm = new Vue({
  	el: '#app',

    router,

    store,

    data () {
    	return {
    		isLoading: false
    	}
    },

    template: '<App/>',

    components: { App },

    methods: {
    	/**
    	 * 获取用户信息
    	 * @return {[type]} [description]
    	 */
    	getUserAndEnterpriseInfo () {
    		return new Promise((resolve, reject) => {
    			this.$http.post(this.api.eabcommon, {
    				action: 'getUserAndEnterpriseInfo'
    			}).then(
    				res => {
    					if (res.body.result == 1) {
    						resolve(res.body);
    					}
    				}
    			);
    		});
    	},

    	/**
    	 * 切换用户当前登录企业
    	 */
    	setDefaultEnterprise (enterpriseId) {
    		return new Promise((resolve, reject) => {
    			this.$http.post(this.api.enterprise, {
    				action: 'setDefaultEnterpriseId',
    				enterpriseId: enterpriseId
    			}).then(
    				res => {
    					if (res.body.result == 1) {
    						resolve(res.body);
    					}
    				}
    			);
    		});
    	}
    }
})

/**
 * vue-resource请求拦截器
 */
Vue.http.interceptors.push((request, next) => {

	request.emulateJSON = request.emulateJSON === undefined ? true : request.emulateJSON;

	vm.isLoading = request.showLoading ? true : false;

	next((response) => {
		vm.isLoading = false;
		if (response.body.result != 1) { // 统一错误状态处理
			// Vue.toasted.show(response.body.errorMsg);
            vm.$toast.show({
                text:res.body.errorMsg,
                translate:true
            })
		}
	});
});

Vue.component('modal', resolve => require(['@/components/modal'], resolve))
Vue.component('loading', resolve => require(['@/components/loading'], resolve))
Vue.component('tt', resolve => require(['@/components/plugin_css_reset'], resolve))
